//
//  DBHelper.m
//  TestARC
//
//  Created by wuxiaoquan on 12-9-26.
//
//

#import "DBHelper.h"

#define dbFileName @"yaoxiadb.db"

@implementation DBHelper

@synthesize databaseFilePath;

- (id)init
{
    self = [super init];
    if (self) {
        self.databaseFilePath = [self getDBFilePath];
        database = [FMDatabase databaseWithPath:self.databaseFilePath];
    }
    return self;
}



-(NSString *)getDBFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString * toPath = [documentsDirectory stringByAppendingPathComponent:dbFileName];
    return toPath;
}

-(BOOL)open
{
    return [database open];
}

-(BOOL)close
{
    return [database close];
}

-(FMResultSet *)quaryCommonDrug
{
    //NSLog(@"dbHelper query!");
    NSString* sql = @"select CommonDrug._id as _id,name,BrandName,CommonName,DrugData.queryid as queryid from CommonDrug,DrugData where CommonDrug.drugId = DrugData._id";//查询语句
    return [database executeQuery:sql];
}

-(FMResultSet *)quaryCommonDisease
{
    //NSLog(@"dbHelper query!");
    NSString* sql = @"select _id,diseaseName as name,queryId as queryid from CommonDisease";//查询语句
    return [database executeQuery:sql];
}

- (FMResultSet *)searchDrugSeggestion:(NSString *)queryText
{
    //查询语句
    NSString* sql = @"SELECT * FROM DrugData WHERE name like ?";
    return [database executeQuery:sql,[NSString stringWithFormat:@"%@%%", queryText]];
}

- (FMResultSet *)searchBothSeggestion:(NSString *)queryText
{
    //查询语句
    NSString* sql = @"select * from ( select _id,name,queryid,'use4disease' as BrandName, 'use4disease' as CommonName from DiseaseData where name like ?  ) union all select * from (select _id,name,queryid,BrandName,CommonName from DrugData where name like ?)";
    return [database executeQuery:sql,[NSString stringWithFormat:@"%@%%", queryText],[NSString stringWithFormat:@"%@%%", queryText]];
}

- (FMResultSet *)searchDrugWithBrandName:(NSString *)queryText
{
    //查询语句
    NSString* sql = @"SELECT * FROM DrugData WHERE BrandName = ?";
    return [database executeQuery:sql,queryText];
}

- (FMResultSet *)searchDiseaseSeggestion:(NSString *)queryText
{
    NSString* sql = @"SELECT * FROM DiseaseData WHERE name like ?";
    return [database executeQuery:sql,[NSString stringWithFormat:@"%@%%", queryText]];
}

- (FMResultSet *)queryDrugsLike:(NSString *)queryId
{
    NSString* sql = @"SELECT *  FROM  DrugData  WHERE queryid  =  ?";
    return [database executeQuery:sql,queryId];
}

@end
